package com.example.syssoft.domain;


import com.example.syssoft.common.entities.LogicEntity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.List;
import java.util.Set;

@Entity
@Getter
@Setter
@Inheritance
@Where(clause = "is_deleted = 0")
@Table(name = "t_shop")
@JsonIgnoreProperties(value = {"handler","hibernateLazyInitializer","fieldHandler"})
public class Shop extends LogicEntity {

    @Column(length = 128)
    @Schema(description = "商店名称")
    private String name;

    @Schema(description = "商店描述")
    @Column(length = 512)
    private String description;

    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnoreProperties(value = {"shops"})
    @Schema(description = "隶属商家")
    private Business business;

    @OneToMany(mappedBy = "shop",fetch = FetchType.LAZY,cascade = {CascadeType.ALL})
    @JsonIgnoreProperties(value = {"shop"})
    @Schema(description = "商店货物")
    private Set<ShopItem> shopItems;

    @OneToMany(mappedBy = "shop",fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    @JsonIgnoreProperties(value = {"shop"})
    @Schema(description = "订单")
    private List<Order> orders;

    public ShopItem addItem(Item item){
        ShopItem shopItem = new ShopItem();
        shopItem.setItem(item);
        shopItem.setShop(this);
        shopItem.setState(0);
        shopItems.add(shopItem);
        return shopItem;
    }

    public void saleItem(ShopItem shopItem){
        shopItem.setState(1);
    }

    public void stopSale(ShopItem shopItem){
        shopItem.setState(0);
    }
}